package com.xjrsoft.module.customer.erp_model.eRP_SalesInvoice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.eRP_SalesInvoice.dto.ErpSalesinvoiceDto;
import com.xjrsoft.module.customer.erp_model.eRP_SalesInvoice.dto.SaveErpSalesinvoiceFormDataDto;
import com.xjrsoft.module.customer.erp_model.eRP_SalesInvoice.vo.ErpSalesinvoiceVo;
import com.xjrsoft.module.customer.erp_model.eRP_SalesInvoice.vo.ErpSalesinvoiceListVo;
import com.xjrsoft.module.customer.erp_model.eRP_SalesInvoice.dto.ErpSalesinvoiceListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.eRP_SalesInvoice.entity.ErpSalesinvoice;
import com.xjrsoft.module.customer.erp_model.eRP_SalesInvoice.vo.ErpSalesinvoiceVo;
import com.xjrsoft.module.customer.erp_model.eRP_SalesInvoice.service.IErpSalesinvoiceService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-10-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_salesinvoice")
@Api(value = "开票管理", tags = "开票管理接口")
public class ErpSalesinvoiceController {


	private final IErpSalesinvoiceService erpSalesinvoiceService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpSalesinvoice")
	public Response<ErpSalesinvoiceVo> getFormData(@PathVariable String id) {
		// 主表数据
		ErpSalesinvoice erpSalesinvoice = erpSalesinvoiceService.getById(id);
		return Response.ok(BeanUtil.copy(erpSalesinvoice, ErpSalesinvoiceVo.class));
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpSalesinvoice")
	public Response<PageOutput<ErpSalesinvoiceListVo>> getPageList(ErpSalesinvoiceListDto listDto) {
		IPage<ErpSalesinvoice> page = erpSalesinvoiceService.getPageList(listDto);
		List<ErpSalesinvoiceListVo> records = BeanUtil.copyList(page.getRecords(), ErpSalesinvoiceListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		//FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpSalesinvoice")
	public Response save(@RequestBody SaveErpSalesinvoiceFormDataDto formDto) {
		ErpSalesinvoiceDto erpSalesinvoiceDto = formDto.getErpSalesinvoiceDto();
		ErpSalesinvoice erpSalesinvoice = BeanUtil.copy(erpSalesinvoiceDto, ErpSalesinvoice.class);
		String dataId = erpSalesinvoiceService.addErpSalesinvoice(erpSalesinvoice);
		if (dataId != null) {
			codeRuleService.useEncode("SalesInvoiCode");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpSalesinvoice")
	public Response update(@PathVariable String id, @RequestBody SaveErpSalesinvoiceFormDataDto formDto) {
		ErpSalesinvoiceDto erpSalesinvoiceDto = formDto.getErpSalesinvoiceDto();
		ErpSalesinvoice erpSalesinvoice = BeanUtil.copy(erpSalesinvoiceDto, ErpSalesinvoice.class);
		return Response.status(erpSalesinvoiceService.updateErpSalesinvoice(id, erpSalesinvoice));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpSalesinvoiceService.removeByIds(Func.toStrList(ids)));
	}

	
}
